<?php
class Wap_Models_ProductModel extends Zend_Db_Table_Abstract{
	protected $_name="product";
	protected $_primary="id";
	
	function getProductList($arrParam){
		$db = Zend_Db_Table::getDefaultAdapter();
		$mySql = $db->select()
					->from(array('p'	=> $this->_name),array('id','name','vote','view','developer','comment','download','icon','like','vote_value'))					
					->where("status = 1")
					->limitPage($arrParam['page'], $arrParam['line_per_page']);
		if (isset($arrParam['category_id'])){
			$mySql->where("category_id = ?" , $arrParam['category_id']);
		}
		if (isset($arrParam['search'])){
			$mySql->where("name like ?" , "%" . $arrParam['search'] . "%");
		}
		if (isset($arrParam['order']) && in_array($arrParam['order'], array('download','id','vote'))){
			$mySql->order($arrParam['order'] . " desc");
		}else{
			$mySql->order("id desc");
		}
		$result = $db->fetchAll($mySql);
		return $result;
	}
	function getProductInfo($id){
		if(!$id){
			return false;
		}
		$db = Zend_Db_Table::getDefaultAdapter();
		$mySql = $db->select()
					->from(array('p'	=> $this->_name),array('*'))		
					->join(array("c"	=> "category"), "p.category_id = c.id",array("category"	=> "name"))			
					->where("status = 1")
					->where("p.id = ?", $id);
		$result = $db->fetchRow($mySql);
		return $result;
	}
	function getRandomProduct($arrParam){
		$db = Zend_Db_Table::getDefaultAdapter();
		$mySql = $db->select()
					->from(array('p'	=> $this->_name),array('id','name','vote','view','developer','comment','download','icon'))					
					->where("status = 1")
					->limitPage($arrParam['page'], $arrParam['line_per_page'])
					->order("RAND()");
		if (isset($arrParam['except'])){
			$mySql->where("id not in (?)",explode(",",$arrParam['except']));
		}
		$result = $db->fetchAll($mySql);
		return $result;
	}
}